package one.bugu.android.demon.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 截图合并操作工具类
 *
 * @author Administrator
 */
public class ScreenshotUtils {

    private final static String FILE_SAVEPATH = Environment
            .getExternalStorageDirectory().getAbsolutePath();
    public static String pathfile = FILE_SAVEPATH + "/ScreenshotUtil.png";
    public static int h = 0;

    /**
     * 截取viewgroup的屏幕
     **/
    public static Bitmap saveScreen(final ViewGroup viewGroup) {
        // 获取viewgroup实际高度
        h = 0;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            h += viewGroup.getChildAt(i).getHeight();
            viewGroup.getChildAt(i).setBackgroundResource(android.R.color.white);
        }
        // 创建对应大小的bitmap
        Bitmap bitmap = Bitmap.createBitmap(viewGroup.getWidth(), h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        viewGroup.draw(canvas);

        return bitmap;
    }

    /**
     * 保存截屏 不带dialog
     *
     * @param activity
     */
    public static void saveScreenShot(Activity activity) {
        saveScreenShot(activity, null);
    }

    /**
     * 保存截屏 带dialog
     *
     * @param activity
     */
    public static Bitmap saveScreenShot(Activity activity, Dialog dialog) {
        View mainView = activity.getWindow().getDecorView();
        mainView.setDrawingCacheEnabled(true);
        mainView.buildDrawingCache();
        //获取状态栏以及底部虚拟按钮高度
        int statusBarHeight = ScreenUtils.getStatusBarHeight(activity);
        int virtualHight = ScreenUtils.getNavigationBarHeight(activity);
        Bitmap bitmap = Bitmap.createBitmap(mainView.getDrawingCache(), 0, statusBarHeight, mainView.getWidth(),
                mainView.getHeight() - statusBarHeight - virtualHight - (int)ScreenUtils.dpToPx(activity, 50));
        //同时保存打开的dialog的截图
        if (dialog != null) {
            View dialogView = dialog.getWindow().getDecorView();
            //货物dialog的位置信息
            int location[] = new int[2];
            mainView.getLocationOnScreen(location);
            int location2[] = new int[2];
            dialogView.getLocationOnScreen(location2);
            dialogView.setDrawingCacheEnabled(true);
            dialogView.buildDrawingCache();
            //获取dialog bitmap
            Bitmap bitmap2 = Bitmap.createBitmap(dialogView.getDrawingCache(), 0, 0, dialogView.getWidth(), dialogView.getHeight());
            Canvas canvas = new Canvas(bitmap);
            //将activity与dialog绘制在一起
            canvas.drawBitmap(bitmap2, location2[0] - location[0], location2[1] - location[1], new Paint());
            mainView.destroyDrawingCache();
            dialogView.destroyDrawingCache();
        }
        return bitmap;
    }
}
